﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using fcwms.Common;
using System.Web.UI.HtmlControls;

public partial class admin_ruku_ruku_list : SessionManage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ChkAdminLevel("sys_inreg", ActionEnum.View.ToString()); //检查权限
            RptBind();
            RukuStorageBind();
            RukuWayBind();
        }
    }

    #region 返回入库状态==========================
    protected string GetRukuStatus(int rukustatus)
    {
        string str = "";
        switch (rukustatus)
        {
            case 0:
                str = "正在入库";
                break;
            default:
                str = "完成";
                break;
        }
        return str;
    }
    #endregion

    #region 入库仓库绑定==========================
    private void RukuStorageBind()
    {
        fcwms.BLL.dt_storage bll = new fcwms.BLL.dt_storage();
        DataTable dt = bll.GetList("").Tables[0];
        selStorage.Items.Clear();
        selStorage.Items.Add(new ListItem("选择入库仓库...", ""));
        foreach (DataRow dr in dt.Rows)
        {
            selStorage.Items.Add(new ListItem(dr["storage_name"].ToString(), dr["storage_name"].ToString()));
        }
    }
    #endregion

    #region 入库方式绑定==========================
    private void RukuWayBind()
    {
        fcwms.BLL.dt_ruku_way bll = new fcwms.BLL.dt_ruku_way();
        DataTable dt = bll.GetList("").Tables[0];
        selRukuWay.Items.Clear();
        selRukuWay.Items.Add(new ListItem("请选择入库方式...", ""));
        foreach (DataRow dr in dt.Rows)
        {
            selRukuWay.Items.Add(new ListItem(dr["ruku_way"].ToString(), dr["ruku_way"].ToString()));//列表显示的字段名称,字段名称对应的值
        }
    }
    #endregion

    #region 组合数据绑定==========================
    public void RptBind()
    {
        string decla_id = txtDeclaID.Text.Trim();
        string ruku_status = this.selStatus.Value;
        string start_time = txtStartTime.Text.Trim();
        string end_time = txtEndTime.Text.Trim();
        string ruku_storage = this.selStorage.Value;
        string transactor = txtTransactor.Text.Trim();
        string ruku_way = this.selRukuWay.Value;

        StringBuilder strSql = new StringBuilder();
        strSql.Append("select id,customs_officer,transactor,remark,decla_id,ruku_time,ruku_way,ruku_storage,customs_broker,unstuffing_group,tallyman,customer,ruku_status ");
        strSql.Append(" FROM dt_ruku where 1=1 ");
        if (txtDeclaID.Text.Trim() != string.Empty)
        {
            strSql.Append("and decla_id=@decla_id ");
        }
        if (this.selStatus.Value.Trim() != string.Empty)
        {
            strSql.Append("and ruku_status=@ruku_status ");
        }
        if(txtStartTime.Text.Trim() != string.Empty && txtEndTime.Text.Trim() != string.Empty)
        {
            strSql.Append("and ruku_time between @start_time and @end_time ");
        }
        if(this.selStorage.Value != string.Empty)
        {
            strSql.Append("and ruku_storage=@ruku_storage ");
        }
        if (txtTransactor.Text.Trim() != string.Empty)
        {
            strSql.Append("and transactor=@transactor ");
        }
        if (this.selRukuWay.Value != string.Empty)
        {
            strSql.Append("and ruku_way=@ruku_way ");
        }
        strSql.Append("order by id desc");

        SqlParameter[] parms = new SqlParameter[] { 
            new SqlParameter("@decla_id",decla_id),
            new SqlParameter("@ruku_status",ruku_status),
            new SqlParameter("@start_time",start_time),
            new SqlParameter("@end_time",end_time),
            new SqlParameter("@ruku_storage",ruku_storage),
            new SqlParameter("@transactor",transactor),
            new SqlParameter("@ruku_way",ruku_way)};
        DataSet ds = DbHelperSQL.Query(strSql.ToString(), parms);

        this.AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;  //DataView的Count属性,AspNetPager的RecordCount属性.
        PagedDataSource pds = new PagedDataSource();
        pds.AllowPaging = true;                                   //启用分页.
        pds.PageSize = AspNetPager1.PageSize;                     //每页要显示的记录条数.      
        pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1; //当前页索引.
        pds.DataSource = ds.Tables[0].DefaultView;                //分页数据源以DataView为数据源.  
        lblCount.Text = AspNetPager1.RecordCount.ToString();      //总共几条数据
        rptList.DataSource = pds;
        rptList.DataBind();

        for (int i = rptList.Items.Count - 1; i > 0; i--)
        {
            //HtmlTableCell oCell_Previous = rptList.Items[i - 1].FindControl("declaid") as HtmlTableCell;
            //HtmlTableCell oCell = rptList.Items[i].FindControl("declaid") as HtmlTableCell;

            //oCell.RowSpan = (oCell.RowSpan == -1) ? 1 : oCell.RowSpan;
            //oCell_Previous.RowSpan = (oCell_Previous.RowSpan == -1) ? 1 : oCell_Previous.RowSpan;

            //if (oCell.InnerText == oCell_Previous.InnerText)
            //{
            //    oCell.Visible = false;
            //    oCell_Previous.RowSpan += oCell.RowSpan;
            //}

            //HtmlTableCell oCell2_Previous = rptList.Items[i - 1].FindControl("customsbroker") as HtmlTableCell;
            //HtmlTableCell oCell2 = rptList.Items[i].FindControl("customsbroker") as HtmlTableCell;

            //oCell2.RowSpan = (oCell2.RowSpan == -1) ? 1 : oCell2.RowSpan;
            //oCell2_Previous.RowSpan = (oCell2_Previous.RowSpan == -1) ? 1 : oCell2_Previous.RowSpan;

            //if (oCell2.InnerText == oCell2_Previous.InnerText)
            //{
            //    oCell2.Visible = false;
            //    oCell2_Previous.RowSpan += oCell2.RowSpan;
            //}
        }
    }


    //protected void ddlNum_SelectedIndexChanged1(object sender, EventArgs e)
    //{
    //    AspNetPager1.CurrentPageIndex = 1;
    //    AspNetPager1.PageSize = Convert.ToInt32(ddlNum.Text.Trim());
    //    RptBind();
    //}
    //每页记录数选择
    protected void btn10_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
        AspNetPager1.PageSize = 10;
        RptBind();
    }
    protected void btn20_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
        AspNetPager1.PageSize = 20;
        RptBind();
    }
    protected void btn50_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
        AspNetPager1.PageSize = 50;
        RptBind();
    }
    //分页绑定
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        RptBind();
    }
    #endregion

    #region 查询重置==============================

    protected void lbtnSearch_Click(object sender, EventArgs e)//查询
    {
        AspNetPager1.CurrentPageIndex = 1;
        RptBind();
    }

    //清空条件
    protected void lbtnReset_Click(object sender, EventArgs e)
    {
        txtDeclaID.Text = string.Empty;
        txtEndTime.Text = string.Empty;
        txtStartTime.Text = string.Empty;
        txtTransactor.Text = string.Empty;
        this.selStatus.Value = string.Empty;
        this.selStorage.Value = string.Empty;
        RptBind();
    }
    #endregion

    #region 删除操作==============================
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        ////ChkAdminLevel("sys_branch", ActionEnum.Delete.ToString()); //检查权限
        fcwms.BLL.dt_ruku bll = new fcwms.BLL.dt_ruku();
        for (int i = 0; i < rptList.Items.Count; i++)
        {
            string decla_id = ((HiddenField)rptList.Items[i].FindControl("hidId")).Value;
            CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
            if (cb.Checked)
            {
                bll.Delete(decla_id);
            }
        }
        MessageBox.Jscript(this, "批量删除成功啦", "ruku_list.aspx", "Success");
    }
    #endregion

    #region 状态修改==============================
    public bool DoEdit2(string _decla_id)
    {
        bool result = true;
        fcwms.BLL.dt_ruku bll = new fcwms.BLL.dt_ruku();
        fcwms.Model.dt_ruku model = bll.GetModel(_decla_id);
        model.decla_id = _decla_id;
        model.ruku_storage = new fcwms.DAL.dt_ruku().GetRukuStorage(_decla_id);

        if (!bll.UpdateStatus(model))
        {
            result = false;
        }
        return result;

    }
    #endregion
    protected void lbtnFinish_Click(object sender, EventArgs e)
    {
        fcwms.BLL.dt_ruku bll = new fcwms.BLL.dt_ruku();
        for (int i = 0; i < rptList.Items.Count; i++)
        {
            string decla_id = ((HiddenField)rptList.Items[i].FindControl("hidId")).Value;
            CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
            if (cb.Checked)
            {
                if (!DoEdit2(decla_id))
                {
                    MessageBox.Jscript(this, "完成入库操作出错啦！", "", "Error");
                    return;
                }
                MessageBox.Jscript(this, "完成入库成功啦！", "ruku_list.aspx", "Success");
            }
        }
    }
}